import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import DataVVue3 from '@kjgl77/datav-vue3'
// 导入technology格CSS
import './assets/tech-style.css'
// 导入DataV风格的MessageBox样式
import './assets/datav-messagebox.css'
import App from './App.vue'

// 导入technology格页面组件
import Login from './views/technology/Login.vue'
import TechDashboard from './views/technology/Dashboard.vue'
import ResumeManagement from './views/technology/ResumeManagement.vue'
import TemplateManagement from './views/technology/TemplateManagement.vue'
import IndustryDataManagement from './views/technology/IndustryDataManagement.vue'
import UserManagement from './views/technology/UserManagement.vue'
import SystemSettings from './views/technology/SystemSettings.vue'
import BubbleTagsManagement from './views/technology/BubbleTagsManagement.vue'
import IndustryResumeTemplates from './views/technology/IndustryResumeTemplates.vue'
import RecommendTemplateManagement from './views/technology/RecommendTemplateManagement.vue'
import SkillsManagement from './views/technology/SkillsManagement.vue'

// 导入tradition风格页面组件
import TraditionalDashboard from './views/tradition/Dashboard.vue'

// 路由配置
const routes = [
  { path: '/login', component: Login, name: '登录' },
  {
    path: '/',
    redirect: '/dashboard',
    meta: { requiresAuth: true }
  },
  {
    path: '/dashboard',
    component: TechDashboard,
    name: '仪表盘',
    meta: { requiresAuth: true }
  },
  {
    path: '/resume',
    component: ResumeManagement,
    name: '简历管理',
    meta: { requiresAuth: true }
  },
  {
    path: '/template',
    component: TemplateManagement,
    name: '模型管理',
    meta: { requiresAuth: true }
  },
  {
    path: '/industry-data',
    component: IndustryDataManagement,
    name: '行业数据管理',
    meta: { requiresAuth: true }
  },
  {
    path: '/industry-data/add',
    component: () => import('./views/technology/IndustryDataAdd.vue'),
    name: '新增行业数据',
    meta: { requiresAuth: true }
  },
  {
    path: '/industry-data/edit/:id',
    component: () => import('./views/technology/IndustryDataEdit.vue'),
    name: '编辑行业数据',
    meta: { requiresAuth: true }
  },
  {
    path: '/bubble-tags',
    component: BubbleTagsManagement,
    name: '首页气泡设置',
    meta: { requiresAuth: true }
  },
  {
    path: '/user',
    component: UserManagement,
    name: '用户管理',
    meta: { requiresAuth: true }
  },
  {
    path: '/industry-resume-templates',
    component: IndustryResumeTemplates,
    name: '行业简历模板',
    meta: { requiresAuth: true }
  },
  {
    path: '/recommend-templates',
    component: RecommendTemplateManagement,
    name: '推荐模板管理',
    meta: { requiresAuth: true }
  },
  {
    path: '/skills-management',
    component: SkillsManagement,
    name: '技能管理',
    meta: { requiresAuth: true }
  },
  // {
  //   path: '/settings',
  //   component: SystemSettings,
  //   name: '系统设置',
  //   meta: { requiresAuth: true }
  // },
  // {
  //   path: '/tech-style',
  //   component: TechDashboard,
  //   name: 'technology格',
  //   meta: { requiresAuth: true, style: 'tech' }
  // },
  // {
  //   path: '/traditional-style',
  //   component: TraditionalDashboard,
  //   name: 'tradition风格',
  //   meta: { requiresAuth: true, style: 'traditional' }
  // },
  // 添加通配符路由，捕获所有无效路径并重定向到首页
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
    name: '404重定向'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.meta.requiresAuth && !token) {
    // 需要登录但未登录，重定向到登录页
    next('/login')
  } else if (to.path === '/login' && token) {
    // 已登录但访问登录页，重定向到首页
    next('/')
  } else {
    // 其他情况正常放行
    next()
  }
})

// 路由错误处理
router.onError((error) => {
  console.error('路由错误:', error)
  router.push('/')
})

const app = createApp(App)

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus)
app.use(DataVVue3)
app.use(router)
app.mount('#app')